package com.wxx;

/**
 * @author Jetwang
 * @version 1.0
 * @date 2022/4/17 21:25
 * LinkedListStack 用链表模拟栈
 */
public class LinkedListStack<T> implements Stack<T> {
    private LinkedList<T> list;

    public LinkedListStack() {
        list = new LinkedList<>();
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public void push(T t) {
        // 从头部插入
        list.add(0, t);
    }

    @Override
    public T pop() {
        // 从头部删除
        T t = list.remove(0);
        return t;
    }

    @Override
    public T peek() {
        // 获取头部元素
        T t = list.get(0);
        return t;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Stack: pop ");
        builder.append(list);
        return builder.toString();
    }
}
